package org.graduation.note.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.graduation.model.common.note.Note;
import org.graduation.model.common.user.User;
import org.graduation.note.mapper.MybatisPlusWrapperFactory;
import org.graduation.note.mapper.NoteMapper;
import org.graduation.note.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note> implements NoteService {
    @Autowired
    private NoteMapper noteMapper;

    //todo permissions
    @Override
    public IPage<Note> pageList(IPage<Note> page, Integer uid) {
        LambdaQueryWrapper<Note> query = new LambdaQueryWrapper<>();
        query.eq(Note::getUid, uid)
                .orderByDesc(Note::getNoteId);
        return noteMapper.selectPage(page, query);
    }

    @Override
    public boolean updateNote(Note note) {
        if (note.getAlarmTime() != null) {
            return noteMapper.updateById(note) == 1;
        } else {
            LambdaUpdateWrapper<Note> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.eq(Note::getNoteId, note.getNoteId())
                    .set(Note::getAlarmTime, null);
            return noteMapper.update(note, updateWrapper) == 1;
        }
    }

    @Override
    public List<Note> alarmNote(User user) {
        return noteMapper.selectList(MybatisPlusWrapperFactory.getAllAlarmNote(user.getUid()));
    }
}
